package com.sub.project.info.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;

import com.sub.common.utils.StringUtils;
import com.sub.project.info.domain.UserTechnology;
import com.sub.project.info.service.IUserTechnologyService;
import com.sub.project.info.vo.RestVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.common.utils.poi.ExcelUtil;
import com.sub.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-01-07
 */
@RestController
@RequestMapping("/system/technology" )
public class UserTechnologyController extends BaseController {
    @Autowired
    private IUserTechnologyService userTechnologyService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:technology:list')")
    @GetMapping("/list" )
    public TableDataInfo list(UserTechnology userTechnology) {
        startPage();
        QueryWrapper<UserTechnology> queryWrapper = new QueryWrapper<UserTechnology>();
                    if (StringUtils.isNotBlank(userTechnology.getName())){
            queryWrapper.like("name" ,userTechnology.getName());
        }
            if(StringUtils.isNotBlank(userTechnology.getContent())){
     queryWrapper.eq("content" ,userTechnology.getContent());
}
    if(userTechnology.getProficiency() != null){
     queryWrapper.eq("proficiency" ,userTechnology.getProficiency());
}
    if(userTechnology.getStartTime() != null){
     queryWrapper.eq("start_time" ,userTechnology.getStartTime());
}
    if(StringUtils.isNotBlank(userTechnology.getCreateByUserId())){
     queryWrapper.eq("create_by_user_id" ,userTechnology.getCreateByUserId());
}
    if(StringUtils.isNotBlank(userTechnology.getUpdateByUserId())){
     queryWrapper.eq("update_by_user_id" ,userTechnology.getUpdateByUserId());
}
    if(userTechnology.getUserId() != null){
     queryWrapper.eq("user_id" ,userTechnology.getUserId());
}
            List<UserTechnology> list = userTechnologyService.list(queryWrapper);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('system:techology:list')" )
    @GetMapping("/appletList" )
    public TableDataInfo appletList(String openId) {
        QueryWrapper<UserTechnology> queryWrapper = new QueryWrapper<UserTechnology>();
        queryWrapper.eq("user_id" ,openId);
        List<UserTechnology> list = userTechnologyService.list(queryWrapper);
        Iterator iterator = list.iterator();
        List<RestVo> restVos = new ArrayList<>();
        while (iterator.hasNext()){
            UserTechnology technology =  (UserTechnology)iterator.next();
            RestVo restVo = (RestVo)technology.clone();
            restVos.add(restVo);
        }
        return getDataTable(restVos);
    }
    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:technology:export')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserTechnology userTechnology) {
        QueryWrapper<UserTechnology> queryWrapper = new QueryWrapper<UserTechnology>(userTechnology);
        List<UserTechnology> list = userTechnologyService.list(queryWrapper);
        ExcelUtil<UserTechnology> util = new ExcelUtil<UserTechnology>(UserTechnology. class);
        return util.exportExcel(list, "technology" );
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:technology:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(userTechnologyService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:technology:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTechnology userTechnology) {
        return toAjax(userTechnologyService.save(userTechnology) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:technology:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTechnology userTechnology) {
        return toAjax(userTechnologyService.updateById(userTechnology) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:technology:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userTechnologyService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
